package casa.depesos.app.com.activity;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.common.base.task.SilentTask;
import com.common.base.task.TaskEngine;
import java.util.Collections;
import java.util.List;

import casa.depesos.app.com.App;
import casa.depesos.app.com.adapter.NoAdapter;
import casa.depesos.app.com.databinding.ActivityNoBinding;
import casa.depesos.app.com.http.bean.BannerBean;
import casa.depesos.app.com.http.data.Banner;
import casa.depesos.app.com.http.req.BannerReq;
import casa.depesos.app.com.it.BannerClickListener;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public class NoActivity extends BaseActivity implements BannerClickListener {
    ActivityNoBinding binding;

    @Override
    protected boolean needPadding() {
        return false;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.top.top.setPadding(0, Utils.getStatusBarHeight(this), 0, 0);
        binding.top.back.setOnClickListener(v -> finish());
        binding.top.title.setText("Resultado de la solicitud");
        binding.list.setLayoutManager(new LinearLayoutManager(this));
        if (Constant.white) {
            String tmp = binding.tv1.getText().toString();
            tmp = tmp.replace("7", "14");
            binding.tv1.setText(tmp);
        }
        Report.show("ACTION_PP_005");
        showLoading();
        TaskEngine.getInstance().submit(new SilentTask("") {
            @Override
            public void runInBackground() throws Exception {
                BannerBean banner = new BannerReq("2").req();
                BannerBean product = new BannerReq("5").req();
                h.post(() -> {
                    show(banner, product);
                });
            }
        });
        binding.toHome.setOnClickListener(v -> finish());
    }

    private void show(BannerBean banner, BannerBean product) {
        if (banner != null && banner.list != null && !banner.list.isEmpty()) {
            Glide.with(this).load(banner.list.get(0).img).addListener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    hideLoading();
                    NoAdapter adapter = new NoAdapter(NoActivity.this, Collections.emptyList(), getBannerList(product));
                    binding.list.setAdapter(adapter);
                    if (adapter.getItemCount() > 0) {
                        binding.linear.setVisibility(View.VISIBLE);
                    }
                    return true;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    hideLoading();
                    Bitmap bm = ((BitmapDrawable) resource).getBitmap();
                    int w = bm.getWidth();
                    int h = bm.getHeight();
                    h = (int) (h * 1f / w * (Utils.getScreenWidth() - Utils.dp2Px(App.getApp(), 30)));
                    NoAdapter adapter = new NoAdapter(NoActivity.this, getBannerList(banner), getBannerList(product), h);
                    binding.list.setAdapter(adapter);
                    if (adapter.getItemCount() > 0) {
                        binding.linear.setVisibility(View.VISIBLE);
                    }
                    return true;
                }
            }).preload();
        } else {
            hideLoading();
            NoAdapter adapter = new NoAdapter(NoActivity.this, Collections.emptyList(), getBannerList(product));
            binding.list.setAdapter(adapter);
        }
    }

    private List<Banner> getBannerList(BannerBean bean) {
        if (bean == null || bean.list == null) {
            return Collections.emptyList();
        }
        return bean.list;
    }

    @Override
    public void onBannerClick(Banner banner) {
        if ("2".equals(banner.source)) {
            Report.click("ACTION_PP_007");
        } else {
            Report.click("ACTION_PP_006");
        }
        WebActivity.toSysWeb(banner.url);
    }
}
